/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdicionarExame.java
 *
 * Created on 04/12/2011, 08:44:49
 */

package view;

import control.ConsultaControler;
import java.awt.Color;
import java.sql.Timestamp;
import java.util.ArrayList;
import model.Exame;

/**
 *
 * @author tais
 */
public class AdicionarExame extends javax.swing.JFrame {
    private Exame exame = null;
    private ArrayList<util.Observador> observers = new ArrayList<util.Observador>();

    public Exame getExame() {
        return exame;
    }

    public void setExame(Exame exame) {
        this.exame = exame;
    }

    /** Creates new form AdicionarExame */
    public AdicionarExame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelNome = new javax.swing.JLabel();
        descricao = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        local = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        valor = new javax.swing.JTextField();
        data = new javax.swing.JButton();
        labelDia = new javax.swing.JLabel();
        marcar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Adicionar exame ou procedimento");

        labelNome.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        labelNome.setText("Nome/Descrição:");

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        jLabel2.setText("Local:");

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        jLabel3.setText("Valor:");

        data.setText("Selecione uma data");
        data.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dataActionPerformed(evt);
            }
        });

        labelDia.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        labelDia.setText("Dia:");

        marcar.setText("marcar");
        marcar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                marcarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(descricao, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE))
                            .addComponent(labelNome)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(local, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(valor, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(45, 45, 45)
                                .addComponent(labelDia)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(data))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(156, 156, 156)
                        .addComponent(marcar, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(descricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(local, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(valor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelDia)
                    .addComponent(data))
                .addGap(49, 49, 49)
                .addComponent(marcar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void marcarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_marcarActionPerformed
       if(descricao.getText().isEmpty()){
           labelNome.setForeground(Color.red);
       }
       else{
           labelNome.setForeground(Color.black);
           if(data.getText().equals("Selecione uma data")){
               labelDia.setForeground(Color.red);
           }
           else{
               labelDia.setForeground(Color.black);
               exame = new Exame();
               exame.setLocal(local.getText());
               exame.setNome(descricao.getText());
               if(valor.getText().isEmpty()){
                   exame.setValor(0);
               }
               else{
                   exame.setValor(Double.parseDouble(valor.getText()));
               }
           
               exame.setData(Timestamp.valueOf(ConsultaControler.getDataEscolhida()+" 00:00:00.0"));
               notificar();
           }
       }
    }//GEN-LAST:event_marcarActionPerformed

    private void dataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dataActionPerformed
        ConsultaControler.selectDay(null, data);
        data.setForeground(Color.black);
    }//GEN-LAST:event_dataActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton data;
    private javax.swing.JTextField descricao;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel labelDia;
    private javax.swing.JLabel labelNome;
    private javax.swing.JTextField local;
    private javax.swing.JButton marcar;
    private javax.swing.JTextField valor;
    // End of variables declaration//GEN-END:variables
   public void notificar(){
        int i;
        for(i=0; i<observers.size(); i++){
            observers.get(i).atualizar(this);
        }
    }

    public void addObservador(util.Observador o){
        observers.add(o);
    }
}
